<template>
  <el-dialog
    v-dialogDrag
    ref="isPopWindows"
    title="提示"
    :visible.sync="isViewPopWindows"
    width="70%"
    :append-to-body="true"
    :close-on-click-modal="false"
    center
    @close="close"
    @opened="open"
  >
    <dialogHeader
      slot="title"
      title="备件订单明细"
      :style-type="$store.state.app.dialogType"
    />
    <div class="formBoxContain">
      <!--
            buttons: 按钮
            froms: 表单
            fromsField: 表单对应的字段
            tableOption: 网格项
            dynamicApiConfigs: 取值
         -->
      <one-table-template
        ref="isPopWindowsTable"
        :dynamic-buttons="tableButtons"
        :dynamic-components="tableComponents"
        :dynamic-form-fields="formField"
        :dynamic-table-cols="tableCols"
        :dynamic-api-config="apiConfig"
        :dynamic-table-defult-height="350"
        :dynamic-is-show-select="false"
        :dynamic-is-show-more-btn="true"
        :dynamic-is-init-table="true"
        :is-show-pagination="false"
      />
    </div>
  </el-dialog>
</template>

<script>
import dialogHeader from '@/components/dialogHeader'
import OneTableTemplate from '@/components/templates/oneTable'
import { formMixins } from '@/components/mixins/formMixins'
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import api from '@/api/vehicleManagement'
import { vehiclePdiPriceInquirys } from '@/api/se/vehiclePdiPriceInquiry'
import { exportExcel } from '@/utils/getTimeString.js'

export default {
  components: {
    dialogHeader,
    OneTableTemplate
  },
  mixins: [oneTableWithViewTemplateMixins, formMixins],
  props: {
    // 是否关闭弹窗
    isViewPopWindows: {
      type: Boolean,
      default: false
    },
    dynamicEditRowData: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      apiConfig: '',
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '订单号:',
              codeField: 'purOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              disabled: true,
              span: 6
            },
            {
              compKey: 'compKey2',
              labelName: '订货日期:',
              // codeField: "startDate,endDate",
              codeField: 'purDate',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              // component: () =>
              //   import("@/components/org/datePicker/andtDatePicker"),
              // type: "YYYY-MM-DD",
              // // dataToObject: { timer: true, thirtyTime: true, hhmmss: true },
              // format: "yyyy-MM-dd",
              isMust: true,
              disabled: true,
              span: 6
            },
            {
              compKey: 'compKey3',
              labelName: '订单类型：',
              // codeField: "purOrderType",
              codeField: 'purOrderTypeCn',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              // component: () => import("@/components/org/commonSelect"),
              // customObj: {
              //   api: vehiclePdiPriceInquirys.queryOrderType,
              // },
              // type: "dropdownList",
              isMul: false,
              isMust: true,
              disabled: true,
              span: 6
            },
            {
              compKey: 'compKey4',
              labelName: 'VIN码',
              codeField: 'VIN',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMul: false,
              isMust: true,
              disabled: true,
              span: 6,
              isShow:false
            },
            // {
            //   compKey: "compKey4",
            //   labelName: "运输方式：",
            //   codeField: "transportModeName",
            //   component: () => import("@/components/org/commonInput"),
            //   type: "inputText",
            //   // component: () => import("@/components/org/LookupValue"),
            //   // oFields: "statusId,statusText",
            //   // options: [
            //   //   { statusId: "", statusText: "全部" },
            //   //   { statusId: "0", statusText: "公路" },
            //   //   { statusId: "1", statusText: "铁路" },
            //   //   { statusId: "2", statusText: "航空" },
            //   //   { statusId: "3", statusText: "快递" },
            //   //   { statusId: "4", statusText: "...其他" },
            //   // ],
            //   // type: "dropdownList",
            //   isMul: false,
            //   isMust: true,
            //   disabled: true,
            // },
            {
              compKey: 'compKey5',
              labelName: '供应商:',
              codeField: 'dlrId',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: false,
              disabled: true,
              span: 6
            }
          ],
      // 动态组件-按钮
      tableButtons: [
        {
          conmpKey: 'btenKey1',
          type: 'primary',
          size: 'small',
          name: 'download',
          text: '附件下载',
          clickEvent: () => this.fileDownload(),
          position: 'left'
        },
        // {
        //   conmpKey: "btenKey1",
        //   type: "primary",
        //   size: "small",
        //   name: "search",
        //   text: "查询",
        //   clickEvent: () => {
        //     this.open();
        //   },
        //   position: "right",
        // },

        // {
        //   conmpKey: "btenKey4",
        //   type: "",
        //   size: "small",
        //   clickEvent: () => this.exportExcel111(),
        //   text: this.$t("sys.button.export"),
        //   name: "export",
        //   position: "right", // 导出
        // },
        // {
        //   conmpKey: "btenKey5",
        //   type: "",
        //   size: "small",
        //   name: "reset",
        //   text: "重置",
        //   clickEvent: () => this.reset(),
        //   position: "right",
        // },
      ],
      formField: this.$utils.getFormField(
        this,
        {
          purOrderCode: this.dynamicEditRowData.purOrderCode,
          startDate: '',
          endDate: '',
          purOrderType: this.dynamicEditRowData.purOrderType,
          // transportModeName: "",
          dlrId: ''
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      ),

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'isCanLackCn',
              label: '可欠拨',
              align: 'center'
            },
            {
              prop: 'purOrderCode',
              label: '订单号',
              align: 'center',
              fixed: true
            },
            {
              prop: 'partNo',
              label: '备件号',
              align: 'center',
              fixed: true
            },
            {
              prop: 'partName',
              label: '备件名称',
              align: 'center',
              fixed: true
            },
            {
              prop: 'unit',
              label: '计量单位',
              align: 'center'
            },
            {
              prop: 'minPurchasePage',
              label: '最小订货数',
              align: 'center'
            },
            {
              prop: 'purPrice',
              label: '单价',
              align: 'center'
            },
            {
              prop: 'purQty',
              label: '订货数量+',
              align: 'center'
            },
            {
              prop: 'purAmount',
              label: '订货金额',
              align: 'center'
            },
            {
              prop: 'sumAccountQty',
              label: '库存数量',
              align: 'center'
            },
            {
              prop: 'sumQty',
              label: '可用库存',
              align: 'center'
            },
            {
              prop: 'validLackQty',
              label: '有效欠拨数量',
              align: 'center'
            }
            // {
            //   prop: "repairOrderCode",
            //   label: "派工单号+",
            //   align: "center",
            // },
            // {
            //   prop: "vin",
            //   label: "VIN码+",
            //   align: "center",
            // },
            // {
            //   prop: "carLicense",
            //   label: "车牌号+",
            //   align: "center",
            // },
            // {
            //   prop: "custName",
            //   label: "客户名称+",
            //   align: "center",
            // },
            // {
            //   prop: "custTel",
            //   label: "客户电话+",
            //   align: "center",
            // },
          ]
    }
  },
  methods: {
    /**
     * @description 关闭弹窗
     * @time 2021、01、22
     */
    // 下载附件
    fileDownload(){
      if(!this.attachmentUrl){
        this.$message({
          message: "无附件",
          type: "warning",
        });
        return false;
      }
      
      const loading = this.$loading({ text: `正在下载附件` });
      this.$requestAPI({
        method: "GET",
        apiUrl: this.attachmentUrl,
        variables:{
          dataType: 'excel'
        }
      })
        .then((res) => {
          this.$utils.downloadFile(res, this.attachmentName);
        })
        .catch((err) => {})
        .finally(() => {
          loading.close();
        });
    },
    close() {
      this.attachmentName=""
      this.attachmentUrl=""
      this.$emit('close')
      this.isViewPopWindows = false
    },
    open() {
      if(this.dynamicEditRowData.VIN){
        this.tableComponents[3].isShow = true
      }else {
        this.tableComponents[3].isShow = false
      }
      this.formField.purOrderCode = this.dynamicEditRowData.purOrderCode
      this.formField.purDate = this.dynamicEditRowData.purDate
      this.formField.purOrderTypeCn = this.dynamicEditRowData.purOrderTypeCn
      this.formField.transportModeName = this.dynamicEditRowData.transportModeName
      this.formField.VIN = this.dynamicEditRowData.VIN
      this.attachmentName = this.dynamicEditRowData.attachmentName
      this.attachmentUrl = this.dynamicEditRowData.attachmentUrl
      const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: vehiclePdiPriceInquirys.queryPartOrderDetailsPage,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          purOrderCode: this.dynamicEditRowData.purOrderCode,
          startDate: this.formField.startDate,
          endDate: this.formField.endDate,
          purOrderType: this.dynamicEditRowData.purOrderType,
          transportModeName: this.dynamicEditRowData.transportModeName,
          dlrId: ''
        }
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then((response) => {
        this.$refs.isPopWindowsTable.list = response.rows
        // if(response.rows[0].vin){

        // }
      })
    },
    exportExcel111() {
      exportExcel(
        this.formField,
        vehiclePdiPriceInquirys.exportPartOrderDetailsDlr.APIUrl,
        vehiclePdiPriceInquirys.exportPartOrderDetailsDlr.ServiceCode,
        '备件订单明细'
      )
    }
  }
}
</script>
<style scoped>
/*.formBoxContain {*/
/*  height: 600px;*/
/*}*/
</style>
